package com.academicsystem.www.controller;

import com.academicsystem.www.model.*;
import com.academicsystem.www.service.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Jacky
 */
@Slf4j
@RestController
@CrossOrigin
public class Controller {

    @Autowired
    private Service service;

    @RequestMapping("/hello")
    public Result hello(){
        System.out.println("Hello World!");
        return Result.success();
    }

    @RequestMapping("/getMajorTable")
    public Result getMajorTable(){
        log.info("获取专业列表");
        List<Major> majorTable = service.getMajorTable();
        System.out.println(majorTable);
        return Result.success(majorTable);
    }

    @RequestMapping("/getLessonList/{majorId}")
    public Result getLessonList(@PathVariable Integer majorId){
        log.info("获取{}号专业的课程列表",majorId);
        List<Lesson> lessons = service.getLessonList(majorId);
        return Result.success(lessons);
    }

    @RequestMapping("/showGraph")
    public Result showGraph(@RequestParam Integer majorId){
        log.info("展示{}号专业计划的有向图",majorId);
        Graph graph = service.showGraph(majorId);
        return Result.success(graph);
    }

    @RequestMapping("/makePlan")
    public Result makePlan(@RequestParam Integer majorId,double limitCredit){
        log.info("为{}号专业制作教学计划",majorId);
        List<Lesson> plan = service.makePlan(majorId,limitCredit);
        return Result.success(plan);
    }

    @RequestMapping("/addMajor")
    public Result addMajor(@RequestParam String majorName){
        log.info("添加专业{}",majorName);
        Integer id = service.addMajor(majorName);
        return Result.success(id);
    }

    @RequestMapping("/deleteMajor")
    public Result deleteMajor(@RequestBody List<Major> majors){
        for (Major major:majors){
            log.info("删除专业:{}",major.getMajorName());
        }
        service.deleteMajor(majors);
        return Result.success();
    }

    @RequestMapping("/addLesson")
    public Result addLesson(@RequestBody Lesson lesson){
        log.info("添加课程:{}",lesson.getName());
        service.addLesson(lesson);
        return Result.success();
    }

    @RequestMapping("/getLessonExcept")
    public Result getLessonExcept(@RequestParam Integer lessonId,Integer majorId){
        log.info("获取{}号专业除了{}号课程之外的课程",majorId,lessonId);
        List<Lesson> lessonExceptList = service.getLessonExcept(lessonId, majorId);
        return Result.success(lessonExceptList);
    }

    @RequestMapping("/addBefore")
    public Result addBefore(@RequestBody SettingLesson settingLesson){
        log.info("为{}号课程设置先修课程",settingLesson.getLessonId());
        service.addBefore(settingLesson);
        return Result.success();
    }
}
